package com.ssmk.module.my.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.example.retrofitlibrary.BaseObserBean2;
import com.example.retrofitlibrary.RetrofitClientUtlis;
import com.ssmk.R;
import com.ssmk.base.MVPBaseActivity;
import com.ssmk.module.shoppingcar.OrderActivity;
import com.ssmk.url.Url;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import chihane.jdaddressselector.AddressSelector;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;
import io.reactivex.annotations.NonNull;
import mlxy.utils.T;

import static com.ssmk.R.id.ed_dizhi;
import static com.ssmk.R.id.ed_lianxifangshi;

/**
 * 新增收收货地址界面
 *
 *
 */
/**
 *
 *  @author 赵星海
 *  @date 2017/9/15
 */
public class NewaddressActivity extends MVPBaseActivity implements OnAddressSelectedListener {

    @BindView(R.id.tv_newaddress_diqu)
    TextView mTvNewaddressDiqu;
    @BindView(R.id.autulayou_xinzdizhi)
    AutoLinearLayout mAutulayouXinzdizhi;
    @BindView(R.id.but_shouhuo_refer)
    Button mButShouhuoRefer;
    @BindView(R.id.top_iv_back)
    ImageView topIvBack;
    @BindView(R.id.top_tv_title)
    TextView topTvTitle;
    @BindView(R.id.top_tv_other)
    TextView topTvOther;
    @BindView(R.id.ed_shouhuoren)
    EditText edShouhuoren;
    @BindView(ed_lianxifangshi)
    EditText edLianxifangshi;
    @BindView(R.id.ed_diqu)
    TextView edDiqu;
    @BindView(ed_dizhi)
    EditText edDizhi;
    @BindView(R.id.rb_shezhi)
    RadioButton rbShezhi;
    @BindView(R.id.getRegion)
    AutoRelativeLayout getRegion;
    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    private SPUtils personInfo;
    boolean flag = false;
    private BottomDialog dialog;
   private String region=null;
    private EditText editText_name;
    private EditText editText_name_phone;
    private EditText editText_address;
    private ArrayList<String> list_dz;

    @OnClick({R.id.tv_newaddress_diqu, R.id.but_shouhuo_refer, R.id.getRegion})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.getRegion:
                flag = true;
                savaData();
                dialog = new BottomDialog(NewaddressActivity.this);
                dialog.setOnAddressSelectedListener(NewaddressActivity.this);
                dialog.show();

                break;
            case R.id.but_shouhuo_refer:

                SharedPreferences sp = getSharedPreferences("sp", MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();
                if (editText_name.getText().toString()!=null&&!editText_name.getText().toString().equals("")) {
                    edit.putString("name", editText_name.getText().toString());
                }else{
                    Toast.makeText(this,"收货人不能为空!",Toast.LENGTH_SHORT).show();

                    return;
                }
                if (editText_name_phone.getText().toString()!=null&&!editText_name_phone.getText().toString().equals("")) {
                    edit.putString("phone", editText_name_phone.getText().toString());

                }else{
                    Toast.makeText(this,"手机号不能为空!",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (editText_address.getText().toString()!=null&&!editText_address.getText().toString().equals("")) {
                    edit.putString("address", editText_address.getText().toString());

                }else{
                    Toast.makeText(this,"收获地址不能为空!",Toast.LENGTH_SHORT).show();
                    return;

                }
                Toast.makeText(this,"添加成功!",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(NewaddressActivity.this, OrderActivity.class);
                intent.putExtra("shoudao",true);
                String[] s = {editText_name.getText().toString(),editText_name_phone.getText().toString(),editText_address.getText().toString()};
                intent.putExtra("shuju",s);
                startActivity(intent);


                //submit();
                break;
        }
    }

    public void savaData() {
        String Shouhuoren = edShouhuoren.getText().toString();
        String Lianxifangshi = edLianxifangshi.getText().toString();
        edDiqu.setText(region);
        String Dizhi = edDizhi.getText().toString();
        boolean b = rbShezhi.isChecked();

        personInfo = new SPUtils("personInfo");
        if (!StringUtils.isEmpty(Shouhuoren)) {
            personInfo.put("Shouhuoren", Shouhuoren);
        }
        if (!StringUtils.isEmpty(Lianxifangshi)) {
            personInfo.put("Lianxifangshi", Lianxifangshi);
        }
        if (!StringUtils.isEmpty(region)) {
            personInfo.put("Diqu", region);
        }

        if (!StringUtils.isEmpty(Dizhi)) {
            personInfo.put("Dizhi", Dizhi);
        }
        personInfo.put("b", b);
    }

    public void getData() {
        String shouhuoren = personInfo.getString("Shouhuoren", null);
        String Lianxifangshi = personInfo.getString("Lianxifangshi", null);
        String Dizhi = personInfo.getString("Dizhi", null);
        boolean b = personInfo.getBoolean("b", false);
        if (!StringUtils.isEmpty(shouhuoren)) {
            edShouhuoren.setText(shouhuoren);
        }
        if (!StringUtils.isEmpty(Lianxifangshi)) {
            edLianxifangshi.setText(Lianxifangshi);
        }
        if (!StringUtils.isEmpty(Dizhi)) {
            edDizhi.setText(Dizhi);
        }
        rbShezhi.setChecked(b);
    }


    public void submit() {
        String Shouhuoren = edShouhuoren.getText().toString();
        String Lianxifangshi = edLianxifangshi.getText().toString();
        String Dizhi = edDizhi.getText().toString();
        boolean b = rbShezhi.isChecked();
        if (!StringUtils.isEmpty(Shouhuoren) && !StringUtils.isEmpty(Lianxifangshi) && !StringUtils.isEmpty(Dizhi)) {
            HashMap<String, String> mapData = new HashMap<>();
            mapData.put(Shouhuoren,Shouhuoren);
            mapData.put(Lianxifangshi,Lianxifangshi);
            mapData.put(Dizhi,Dizhi);
             RetrofitClientUtlis.getInstance(this, Url.BASEURL3).createBaseApi().get(Url.BASEURL2,mapData , new BaseObserBean2(this){
                 @Override
                 public void onNext(@NonNull Object o) {

                 }

                 @Override
                 public void Next(Object data) {

                 }
             });
            Toast.makeText(NewaddressActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(NewaddressActivity.this, "您的输入有误，请完善信息", Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void UpData(Object o) {

    }

    @Override
    protected void initview() {

        AddressSelector selector = new AddressSelector(this);
        selector.setOnAddressSelectedListener(this);
//        selector.setAddressProvider(new TestAddressProvider());
        assert frameLayout != null;
        frameLayout.addView(selector.getView());
        assert getRegion != null;

        topTvTitle.setText("新增收货地址");
        if (flag) {
            getData();
            flag = false;
        }
        //Todo 保存收货地址
        editText_name = (EditText) findViewById(R.id.ed_shouhuoren);
        editText_name_phone = (EditText) findViewById(ed_lianxifangshi);
        editText_address = (EditText) findViewById(R.id.ed_dizhi);



    }



    @Override
    protected void initdata() {

    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_newaddress;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {

        String s =
                (province == null ? "" : province.name) +
                        (city == null ? "" : "\t" + city.name) +
                        (county == null ? "" : "\t" + county.name) +
                        (street == null ? "" : "\t" + street.name);
        this.region=s;

        Log.i("=====", "onAddressSelected: "+s);

        T.showShort(this, s);
        dialog.cancel();
    }
}
